package com.add;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

public class AddServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		JSONObject message=new JSONObject();
		HttpSession session = request.getSession();
		System.out.println(session.getAttribute("uEmail"));
		if(session.getAttribute("uEmail")!=null){
			
			ArrayList list=new ArrayList();
			ArrayList list1=new ArrayList();
			int Post_ID=-1;
			int jobcount=Integer.valueOf(request.getParameter("jobcount"));
//			String jobOffer = new String(request.getParameter("jobOffer").getBytes("ISO8859-1"),"UTF-8");
			String jobOffer =request.getParameter("jobOffer");
			Post post=new Post();
			String uId=String.valueOf(session.getAttribute("uId"));
			post.setAuthorId(Integer.valueOf(uId));
			//获取时间
			System.out.println("提供的工作数目："+jobcount);
			Date date1=new Date();
			SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//���ø�ʽ
			String dateTime = formater.format(date1);
			post.setDate(dateTime);
			
			post.setJobCount(jobcount);
			post.setPlace(jobOffer);
			
			PostImplADD pi=new PostImplADD();
			Post_ID=pi.add(post);
			list1.add(jobcount);
			list1.add(jobOffer);
			list.add(list1);
			for(int i=0;i<jobcount;i++){
				ArrayList list2=new ArrayList();
				String label="工作"+(i+1);
				list2.add(label);
				
				String jobname=request.getParameter("jobname"+(i+1));
				int state=Integer.valueOf(request.getParameter("state"+(i+1)));
				String jobtype=request.getParameter("job_type"+(i+1));
				String paytype=request.getParameter("pay_type"+(i+1));
				int salary=Integer.valueOf(request.getParameter("salary"+(i+1)));
				String place1=request.getParameter("s_province"+(i+1));
				String place2=request.getParameter("s_county"+(i+1));
				String place3=request.getParameter("s_city"+(i+1));
				String place4=request.getParameter("job_palce"+(i+1));
				String place=place1+"   "+place3+"   "+place2+"   "+place4;
				String notice=request.getParameter("job_notice"+(i+1));
				System.out.println("工作提供单位："+jobOffer);
				if(paytype.equals("月结") && Post_ID>=0){
					Job2ImplAdd j2=new Job2ImplAdd();
					
					String date=new String(request.getParameter("job"+(i+1)+"_date1").getBytes("ISO8859-1"),"UTF-8")+"    至   "+new String(request.getParameter("job"+(i+1)+"_date2").getBytes("ISO8859-1"),"UTF-8");
						String time1=request.getParameter("job"+(i+1)+"_time3")+"    至   "+request.getParameter("job"+(i+1)+"_time4");
						String time2=request.getParameter("job"+(i+1)+"_time5")+"    至   "+request.getParameter("job"+(i+1)+"_time6");
						String time3=request.getParameter("job"+(i+1)+"_time7")+"    至   "+request.getParameter("job"+(i+1)+"_time8");

					Job2 job = new Job2();
					job.setState(state);
					job.setDate(date);
					job.setJobName(jobname);
					job.setJobType(jobtype);
					job.setNotcie(notice);
					job.setPayType(paytype);
					job.setPlace(place);
					job.setSalary(salary);
					if(!time1.equals("")){
						job.setTime1(time1);
					}
					if(!time2.equals("")){
						job.setTime2(time2);
					}if(!time3.equals("")){
						job.setTime3(time3);
					}
					job.setPostId(Post_ID);
					j2.add(Post_ID, job);
				}
				else{
					String worktime=request.getParameter("job"+(i+1)+"_time1")+"    至   "+request.getParameter("job"+(i+1)+"_time2");
					Job1ImplAdd j1=new Job1ImplAdd();
					
					Job1 job = new Job1();
					job.setState(state);
					job.setJobName(jobname);
					job.setJobType(jobtype);
					job.setNotcie(notice);
					job.setPayType(paytype);
					job.setPlace(place);
					job.setPostId(Post_ID);
					job.setSalary(salary);
					job.setWorkTime(worktime);
					
					j1.add(Post_ID, job);
				}
				
			}
			message.put("message", "发布成功");
			
		}
		else{
			message.put("message", "请先登录！");
		}
		response.getWriter().print(message);  
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
